package com.weaver.drools.common.redis.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.weaver.drools.common.redis.entity.OperateLog;
import com.weaver.drools.common.redis.entity.RedisInfoDetail;
import com.weaver.drools.common.redis.service.RedisService;
import com.weaver.drools.common.utils.ControllerUtil;
import com.weaver.drools.common.utils.ReturnMessage;

@Controller
public class RedisController {
	
	@Autowired
	RedisService redisService;
	
	//跳转到监控页面
	@RequestMapping(value="redisMonitor",method = RequestMethod.GET)
	public String redisMonitor(HttpServletResponse response, HttpServletRequest request) {
		//获取redis的info
		List<RedisInfoDetail> ridList = redisService.getRedisInfo();
		//获取redis的日志记录
		List<OperateLog> logList = redisService.getLogs(1000);
		//获取日志总数
		long logLen = redisService.getLogLen();
		request.setAttribute("infoList", ridList);
		request.setAttribute("logList", logList);
		request.setAttribute("logLen", logLen);
		return "redis/redisMonitor";
	}
	
	//清空日志按钮
	@RequestMapping(value ="logEmpty",produces="application/json;charset=UTF-8")
	@ResponseBody
	public void logEmpty(HttpServletResponse response, HttpServletRequest request){
		String cs = "";
		try {
			redisService.logEmpty();
			cs = ReturnMessage.messageData(ReturnMessage.SUCCESS, "日志清空成功！",null);
		} catch (Exception e) {
			cs = ReturnMessage.messageData(ReturnMessage.FAIL, "日志清空失败！",null);
		}
		ControllerUtil.writejsonAndSession(response,request,cs);
	}
	
	//获取当前数据库中key的数量
	@RequestMapping(value="getKeysSize",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String getKeysSize(){
		return JSON.toJSONString(redisService.getKeysSize());
	}
	
	//获取当前数据库内存使用大小情况
	@RequestMapping(value="getMemeryInfo",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String getMemeryInfo(){
		return JSON.toJSONString(redisService.getMemeryInfo());
	}
}
